package com.lagou;

import com.lagou.rocketmq.service.ProductStockService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootApplication
@MapperScan(basePackages = "com.lagou.rocketmq.mapper")
public class RocketmqHomeworkApplication implements CommandLineRunner {

	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private ProductStockService productStockService;

	public static void main(String[] args) {
		SpringApplication.run(RocketmqHomeworkApplication.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
		redisTemplate.delete(redisTemplate.keys("*"));
		//初始化redis数据
		productStockService.findAllStock().forEach(productStock -> {
			redisTemplate.opsForValue().set(productStock.getProductId(), productStock.getProductNum());
		});
	}
}
